package com.xuecheng.manage_course.controller;

import com.xuecheng.api.course.CourseControlllerApi;
import com.xuecheng.framework.domain.course.CoursePic;
import com.xuecheng.framework.domain.course.Teachplan;
import com.xuecheng.framework.domain.course.TeachplanMedia;
import com.xuecheng.framework.domain.course.ext.CourseView;
import com.xuecheng.framework.domain.course.ext.TeachplanNode;
import com.xuecheng.framework.domain.course.request.CourseListRequest;
import com.xuecheng.framework.domain.course.response.CoursePublishResult;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.utils.XcOauth2Util;
import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/course")
public class CourseController extends BaseController implements CourseControlllerApi {
    @Autowired
    private CourseService courseService;


    @GetMapping("/teachplan/list/{courseId}")
    public TeachplanNode findTeachplanList(@PathVariable("courseId") String courseId) {
        return courseService.findTeachplanList(courseId);
    }


    @PostMapping("/teachplan/add")
    public ResponseResult addTeachplan(@RequestBody Teachplan teachplan) {
        return courseService.addTeachplan(teachplan);
    }


    @GetMapping("/coursebase/list/{currentPage}/{pageSize}")
    public QueryResponseResult findCourseList(@PathVariable("currentPage") int currentPage,@PathVariable("pageSize") int pageSize,CourseListRequest courseListRequest) {
        //Day_18@15: 加细粒度授权
        //用户所属的公司id从cookie中获取   使用工具类
        XcOauth2Util xcOauth2Util = new XcOauth2Util();
        XcOauth2Util.UserJwt userJwtFromHeader = xcOauth2Util.getUserJwtFromHeader(request);
        //获取公司id
        String companyId1 = userJwtFromHeader.getCompanyId();
        //当前用户所属单位的id
        String companyId = companyId1;
        return courseService.findCourseList(companyId,currentPage,pageSize,courseListRequest);
    }

    /**
     * 添加课程图片
     * @param courseId  课程分类的模块标识符
     * @param pic   图片在mongodb的filesystem中的id
     * @return    基本返回:三件套  是否成功 操作代码  提示信息
     */
    @PostMapping("/coursepic/add")
    public ResponseResult addCouresePic(@RequestParam("courseId")String courseId, @RequestParam("pic") String pic) {
        return courseService.saveCoursePic(courseId,pic);
    }

    /**
     * 获取课程基础信息
     * @param courseid   课程分类的模块标识符
     * @return   Course类,图片id+分类id
     */
    @GetMapping("/coursepic/list/{courseId}")
    public CoursePic findCoursePic(@PathVariable("courseId") String courseid) {
        return courseService.findCoursePic(courseid);
    }

    /**
     * 删除课程图片
     * @param courseId 课程分类的模块标识符
     * @return   基本返回:三件套  是否成功 操作代码  提示信息
     */
    @DeleteMapping("/coursepic/delete")
    public ResponseResult deleteCourse(@RequestParam("courseId") String courseId) {
        return courseService.deleteCoursePic(courseId);
    }

    /**
     * 根据页面ID查询,课程信息
     * @param id  页面ID
     * @return  课程详情页面所需要信息
     */
    @GetMapping("/courseview/{id}")
    public CourseView coureseview(@PathVariable("id") String id) {
        return courseService.getCoruseView(id);
    }

    /**
     *根据页面id,进行课程详情页面浏览
     * @param id    页面id
     * @return  url页面访问路径
     */
    @PostMapping("/preview/{id}")
    public CoursePublishResult preview(@PathVariable("id")String id) {
        return courseService.preview(id);
     }

    /**
     * 课程发布
     * @param id  课程id
     * @return  课程url
     */
    @PostMapping("/publish/{id}")
    public CoursePublishResult publish(@PathVariable String id){
        return courseService.publish(id);
    }



    /**
     * Day14:课程计划与视频产生关系
     * @param teachplanMedia
     * @return
     */
    @PostMapping("/savemedia")
    public ResponseResult savemedia(@RequestBody TeachplanMedia teachplanMedia) {
        return courseService.savemedia(teachplanMedia);
    }

}
